package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.mapper.IncomeMapper;
import com.neuedu.pojo.Income;
import com.neuedu.service.IIncomeService;
import com.neuedu.util.CommonResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wk
 * @since 2020-09-14
 */
@Service
public class IncomeServiceImpl extends ServiceImpl<IncomeMapper, Income> implements IIncomeService {

    @Override
    public CommonResult list(Income income) {
        QueryWrapper<Income> qu=new QueryWrapper<>();
        if(!StringUtils.isBlank(income.getItem())){
            qu.like("item",income.getItem());
        }
        if(!StringUtils.isBlank(income.getDonator())){
            qu.like("donator",income.getDonator());
        }
        if(income.getDonatorType()!=null&&income.getDonatorType()!=1000){
            qu.eq("donator_type",income.getDonatorType());
        }
        if(income.getIncomeType()!=null&&income.getIncomeType()!=1000){
            qu.eq("income_type",income.getIncomeType());
        }
        if(income.getActive()!=null){
            qu.eq("active",income.getActive());
        }
        if(income.getSumRange()!=null&&income.getSumRange()!=1000){
            ToSum(qu,income.getSumRange());
        }
        if(income.getDateRange()!=null&&income.getDateRange().length>0&&income.getDateRange()[0]!=null){
            LocalDate[] array=income.getDateRange();
            qu.between("income_date",array[0],array[1]);
        }
        if(!income.isDis()){
            if(income.getCheckList()!=null&&income.getCheckList().length>0){
                String[] arr=income.getCheckList();
                for(String str:arr){
                    ToFlag(qu,str,income);
                }
            }
            if(income.getIfFriend()!=null){
                qu.eq("if_friend",income.getIfFriend());
            }
            if(income.getIfSpecial()!=null){
                qu.eq("if_special",income.getIfSpecial());
            }
            if(income.getIfReceipt()!=null){

                qu.eq("if_receipt",income.getIfReceipt());
            }
            if(income.getIfsReceipt()!=null){

                qu.eq("ifs_receipt",income.getIfsReceipt());
            }
            if(income.getIfBook()!=null){

                qu.eq("if_book",income.getIfBook());
            }
            if(income.getIfsBook()!=null){

                qu.eq("ifs_book",income.getIfsBook());
            }
        }

        if(income.getIfpage()==1){
            return CommonResult.success(this.page(new Page<Income>(income.getPageNo(), income.getPageSize()),qu));
        }else {
            return CommonResult.success(this.list(qu));
        }

    }
    //计算金额范围
    private void ToSum(QueryWrapper<Income> qu,Integer i){
        switch (i){
            //0~5000
            case 0 :
                qu.between("sum",0,5000);
                break;

            //5001~10000
            case 1 :
                qu.between("sum",5001,10000);
                break;

            //10001~50000
            case 2 :
                qu.between("sum",10001,50000);
                break;

            //50001~10W
            case 3 :
                qu.between("sum",50001,100000);
                break;

            //10w+
            case 4 :
                qu.ge("sum",100000);
                break;
        }
    }
    //处理勾选组
    private void ToFlag(QueryWrapper<Income> qu,String str,Income income){
        switch (str){
            case "ifFriend" :
                qu.eq("if_friend",1);
                income.setIfFriend(1);
                break;
            case "ifSpecial" :
                qu.eq("if_special",1);
                income.setIfSpecial(1);
                break;
            case "ifCountry" :
                qu.eq("if_country",1);
                income.setIfCountry(1);
                break;
            case "ifReceipt" :
                qu.eq("if_receipt",1);
                income.setIfReceipt(1);
                break;
            case "ifsReceipt" :
                qu.eq("ifs_receipt",1);
                income.setIfsReceipt(1);
                break;
            case "ifBook" :
                qu.eq("if_book",1);
                income.setIfBook(1);
                break;
            case "ifsBook" :
                qu.eq("ifs_book",1);
                income.setIfsBook(1);
                break;
        }
    }


    @Override
    public CommonResult del(Integer id) {
        Income income=new Income();
        income.setActive(0);
        income.setId(id);
        if(this.updateById(income)){
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult delMore(Integer[] ids) {
        List<Income> list=new ArrayList<>(10);
        for(Integer id: ids){
           Income income=new Income();
           income.setId(id);
           income.setActive(0);
           list.add(income);
        }
        boolean i=this.updateBatchById(list);
        if(i){
            return CommonResult.success("批量删除成功");
        }
        return CommonResult.failed();
    }
}
